package com.sky.service.impl;

import com.baomidou.mybatisplus.core.conditions.query.LambdaQueryWrapper;
import com.baomidou.mybatisplus.core.conditions.query.QueryWrapper;
import com.baomidou.mybatisplus.core.conditions.update.LambdaUpdateWrapper;
import com.baomidou.mybatisplus.core.metadata.IPage;
import com.baomidou.mybatisplus.extension.plugins.pagination.Page;
import com.sky.constant.MessageConstant;
import com.sky.constant.PasswordConstant;
import com.sky.constant.StatusConstant;
import com.sky.dto.DishPageQueryDTO;
import com.sky.dto.SetmealDTO;
import com.sky.entity.*;
import com.sky.exception.DeletionNotAllowedException;
import com.sky.mapper.CategoryMapper;
import com.sky.mapper.SetmealDishMapper;
import com.sky.mapper.SetmealMapper;
import com.sky.result.PageResult;
import com.sky.service.CategoryService;
import com.sky.service.SetmealService;
import com.sky.vo.SetmealVO;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.BeanUtils;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import org.springframework.util.DigestUtils;

import java.util.List;

/**
 * ClassName: SetmealImpl
 * Package: IntelliJ IDEA
 *
 * @author 张文豪
 * @description
 * @date 2023/8/17 18:00
 */
@Service
@Slf4j
public class SetmealImpl implements SetmealService {
    @Autowired
    private SetmealMapper setmealMapper;
    @Autowired
    private CategoryMapper categoryMapper;
    @Autowired
    private SetmealDishMapper setmealDishMapper;
    @Override
    public List<Setmeal> selectByCategory(Long categoryId) {
        LambdaQueryWrapper<Setmeal> wrapper = new LambdaQueryWrapper<>();
        wrapper.eq(categoryId != null,Setmeal::getCategoryId,categoryId).eq(Setmeal::getStatus,StatusConstant.ENABLE);
        List<Setmeal> list = setmealMapper.selectList(wrapper);
        return list;
    }

    @Override
    public void save(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmeal.setStatus(StatusConstant.ENABLE);
        setmealMapper.insert(setmeal);
        log.info("setmeal id值为：{}",setmeal.getId());


        //为套餐餐品表添加数据
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            log.info("setmeal id值为：{}",setmeal.getId());
            setmealDish.setSetmealId(setmeal.getId());
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public PageResult pageQuery(DishPageQueryDTO dishPageQueryDTO) {
        Page<Setmeal> myPage = new Page<>(dishPageQueryDTO.getPage(),dishPageQueryDTO.getPageSize(),true);

        LambdaQueryWrapper<Setmeal> lambdaQueryWrapper = new LambdaQueryWrapper<>();


        lambdaQueryWrapper.like(dishPageQueryDTO.getName() != null,Setmeal::getName,dishPageQueryDTO.getName());

        IPage<Setmeal> iPage = setmealMapper.selectPage(myPage,lambdaQueryWrapper);

        PageResult pageResult = new PageResult();
        pageResult.setTotal(iPage.getTotal());
        pageResult.setRecords(iPage.getRecords());
        return pageResult;
    }

    @Override
    public void startOrStop(int status, long id) {
        LambdaUpdateWrapper<Setmeal> wrapper = new LambdaUpdateWrapper<>();
        wrapper.eq(Setmeal::getId,id).set(Setmeal::getStatus,status);
        setmealMapper.update(null,wrapper);
    }

    @Override
    public SetmealVO getById(long id) {
        Setmeal setmeal = setmealMapper.selectById(id);
        SetmealVO setmealVO = new SetmealVO();
        BeanUtils.copyProperties(setmeal,setmealVO);
        //将分类的名字给vo
        String name = categoryMapper.selectById(setmeal.getCategoryId()).getName();
        setmealVO.setCategoryName(name);
        //将套餐的菜品给vo
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        List<SetmealDish> setmealDishes = setmealDishMapper.selectList(lambdaQueryWrapper);
        setmealVO.setSetmealDishes(setmealDishes);
        return setmealVO;
    }

    @Override
    public void updateSetmeal(SetmealDTO setmealDTO) {
        Setmeal setmeal = new Setmeal();
        BeanUtils.copyProperties(setmealDTO,setmeal);
        setmealMapper.updateById(setmeal);
        //删除套餐和其菜品相关的数据
        Long id = setmealDTO.getId();
        LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
        lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
        setmealDishMapper.delete(lambdaQueryWrapper);
        //修改的数据加上去
        for (SetmealDish setmealDish : setmealDTO.getSetmealDishes()) {
            setmealDishMapper.insert(setmealDish);
        }
    }

    @Override
    public void deleteByIds(List<Long> ids) {
        for (Long id : ids) {
            Setmeal setmeal =  setmealMapper.selectById(id);
            //当前套餐是否起售
            if(setmeal.getStatus() == StatusConstant.ENABLE){
                throw new DeletionNotAllowedException(MessageConstant.DISH_ON_SALE);
            }
            //删除套餐和其菜品相关的数据
            LambdaQueryWrapper<SetmealDish> lambdaQueryWrapper = new LambdaQueryWrapper<>();
            lambdaQueryWrapper.eq(SetmealDish::getSetmealId,id);
            setmealDishMapper.delete(lambdaQueryWrapper);
        }
        //删除套餐
        setmealMapper.deleteBatchIds(ids);


    }
}
